// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Rand.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework
{
    #region Using Directives

    using System;

    #endregion

    /// <summary>
    /// The rand.
    /// </summary>
    public class Rand
    {
        #region Constants and Fields

        /// <summary>
        /// The random.
        /// </summary>
        private static readonly Random random = new Random((int)Timings.Now.Ticks);

        #endregion

        #region Public Methods

        /// <summary>
        /// The next double.
        /// </summary>
        /// <returns>
        /// The next double.
        /// </returns>
        public static double NextDouble()
        {
            var d = random.NextDouble();
            return d;
        }

        /// <summary>
        /// The next gaussian.
        /// </summary>
        /// <returns>
        /// The next gaussian.
        /// </returns>
        public static double NextGaussian()
        {
            double w, u, v;
            do
            {
                u = (2.0 * NextDouble()) - 1.0;
                v = (2.0 * NextDouble()) - 1.0;
                w = (u * u) + (v * v);
            }
            while (w >= 1.0);

            w = Math.Sqrt((-2.0 * Math.Log(w)) / w);

            // u*w ~ N(0,1)
            // v*w ~ N(0,1)
            return u * w;
        }

        /// <summary>
        /// The next int.
        /// </summary>
        /// <param name="max">
        /// The max.
        /// </param>
        /// <returns>
        /// The next int.
        /// </returns>
        public static int NextInt(int max)
        {
            var d = random.NextDouble();
            return (int)(d * max);
        }

        #endregion
    }
}